﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;

namespace Ciripescu_WebRole
{
    public partial class rss : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string userName = null;
            string page = "1";

            if (Request.QueryString["userName"]!=null)
                userName = Request.QueryString["userName"];
            else
            {
                return;
            }

            if (Request.QueryString["page"]!=null)
                page = Request.QueryString["page"];
            
            // Clear any previous output from the buffer
            Response.Clear();
            Response.ContentType = "text/xml";
            XmlTextWriter xtwFeed = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            xtwFeed.WriteStartDocument();

            // The mandatory rss tag
            xtwFeed.WriteStartElement("rss");
            xtwFeed.WriteAttributeString("version", "2.0");

            // The channel tag contains RSS feed details
            xtwFeed.WriteStartElement("channel");
            xtwFeed.WriteElementString("title", "Ciripescu.ro / "+userName);
            xtwFeed.WriteElementString("link", "http://www.ciriperscu.ro/u/"+userName);
            xtwFeed.WriteElementString("description", "Ultimele ciripuri ale lui "+userName);
            xtwFeed.WriteElementString("copyright", "Copyright 2009 ciripescu.ro");

            CiripManager cm = new CiripManager();
            List<Cirip> lista = cm.GetCiripsFromUserName(userName, Convert.ToInt32(page));

            // add items

            foreach (Cirip c in lista)
            {
                xtwFeed.WriteStartElement("item");
                string continut = c.Content;
                if (!string.IsNullOrEmpty(c.InReplyToUser))                
                    continut = "@" + c.InReplyToUser + ": " + continut;
                
                xtwFeed.WriteElementString("title", continut);
                xtwFeed.WriteElementString("description", continut);
                //xtwFeed.WriteElementString("link", "http://www.ciripescu.ro/cirip.aspx?userName=" + c.getUser()+"&id="+c.RowKey);
                xtwFeed.WriteElementString("link", "http://www.ciripescu.ro/cirip/" + c.getUser() + "/" + c.RowKey);
                xtwFeed.WriteElementString("pubDate", BuildPubDate(c.TimePosted));

                xtwFeed.WriteEndElement();

            }

            

            // Close all tags

            xtwFeed.WriteEndElement();
            xtwFeed.WriteEndElement();
            xtwFeed.WriteEndDocument();
            xtwFeed.Flush();
            xtwFeed.Close();
            Response.End();
        }

        public string BuildPubDate(DateTime d)
        {
            try
            {
                string RV;
                string day = d.Day.ToString();
                if (day.Length == 1) day = "0" + day;
                string month = d.Month.ToString();
                switch (month)
                {
                    case "1":
                        month = "January"; break;
                    case "2":
                        month = "February"; break;
                    case "3":
                        month = "March"; break;
                    case "4":
                        month = "April"; break;
                    case "5":
                        month = "May"; break;
                    case "6":
                        month = "June"; break;
                    case "7":
                        month = "July"; break;
                    case "8":
                        month = "August"; break;
                    case "9":
                        month = "September"; break;
                    case "10":
                        month = "October"; break;
                    case "11":
                        month = "November"; break;
                    case "12":
                        month = "December"; break;
                }

                string Mtime = "";
                DateTime mDate = d.ToUniversalTime();
                if (mDate.Hour.ToString().Length == 1)
                    Mtime = "0" + mDate.Hour.ToString();
                else
                    Mtime = mDate.Hour.ToString();

                Mtime += ":";
                if (mDate.Minute.ToString().Length == 1)
                    Mtime += "0" + mDate.Minute.ToString();
                else
                    Mtime += mDate.Minute.ToString();

                Mtime += ":";
                if (mDate.Second.ToString().Length == 1)
                    Mtime += "0" + mDate.Second.ToString();
                else
                    Mtime += mDate.Second.ToString();


                RV = d.DayOfWeek.ToString().Substring(0, 3);
                RV += ", " + day + " " + month.Substring(0, 3);
                RV += " " + d.Year.ToString() + " " + Mtime + " GMT";
                return RV;
            }
            catch (Exception)
            {
                // nu-mi pasa

                return "";
            }
        }

    }
}
